package com.daniel.cms.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MyBatisPlus配置
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/20 17:19
 */
@MapperScan("com.daniel.cms.mapper")
@EnableTransactionManagement
@Configuration
public class MyBatisPlusConfig {
	/**
	 * 注册乐观锁插件
	 *
	 * @return OptimisticLockerInterceptor
	 */
	@Bean
	public OptimisticLockerInterceptor optimisticLockerInterceptor() {
		return new OptimisticLockerInterceptor();
	}

	/**
	 * 分页插件
	 *
	 * @return PaginationInterceptor
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}

	/**
	 * 逻辑删除组件
	 *
	 * @return ISqlInjector
	 */
	@Bean
	public ISqlInjector sqlInjector() {
		return new LogicSqlInjector();
	}

	/**
	 * SQL执行效率插件
	 * 设置dev、test环境开启
	 *
	 * @return PerformanceInterceptor
	 */
	@Bean
	@Profile({"dev", "test"})
	public PerformanceInterceptor performanceInterceptor() {
		PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
		// sql执行的最大时间，超过则不执行
		performanceInterceptor.setMaxTime(1000);
		// 是否格式化sql代码
		performanceInterceptor.setFormat(true);
		return performanceInterceptor;
	}
}
